<?php 
class DomFgController extends DomController {
	
	public function index() {}
	
	public function when() {
		$view = new DomView('DomFg/whenMakingForms.html');
		$view->banks = DomBankLink::getLinks();
		$panel = array(
			'title' => 'When making forms...',
			'bodyStyle' => 'padding:10px',
			'html' => $view->fetch(),
		);
				
		$this->view->contents = DomFg::render( $panel );
	}
	
	public function panel() {
		$this->view->addPartitioner('contents', 'DomFg/Source/Panels.html');
		
		$panel = array(
			'type' => 'panel', // panel is also default, so if type is not specified panel is assumed
			'layout' => 'table',
			'title' => 'Main panel',
			'layout' => 'table',
			'items' => array(
				
				array(
					'type' => 'panel',
					'title' => 'Sub panel',
					'bodyStyle' => 'height:100px;padding:10px;',
					'border' => true,
					'html' => 'This is left panel',
				),
				
				array(
					'type' => 'panel',
					'title' => 'Sub panel',
					'bodyStyle' => 'height:100px;padding:10px;',
					'border' => true,
					'html' => 'This is right panel',
				),
				
				array(
					'type' => 'fieldSet',
					'title' => 'Fieldset panel',
					'bodyStyle' => 'height:100px;padding:10px;',
					'colspan' => 2,
					'html' => 'This is fieldset panel',
				),
				
			),
		);
		
		$outerPanel = array(
			'type' => 'panel',
			'html' => 'Outer panel',
			'bodyStyle' => 'height:100px;padding:10px;',
		);
		
		$borderlessPanel = array(
			'type' => 'panel',
			'border' => false,
			'frame' => false,
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 3,
			),
			'items' => array(
				array(
					'type' => 'panel',
					'title' => 'Sub panel',
					'bodyStyle' => 'height:100px;padding:10px;',
					'border' => true,
					'frame' => false,
					'html' => 'This is left panel',
				),
				
				array(
					'type' => 'panel',
					'title' => 'Sub panel',
					'bodyStyle' => 'height:100px;padding:10px;',
					'border' => true,
					'frame' => false,
					'html' => 'This is middle panel',
				),
				
				array(
					'type' => 'panel',
					'title' => 'Sub panel',
					'bodyStyle' => 'height:100px;padding:10px;',
					'border' => true,
					'frame' => false,
					'html' => 'This is right panel',
				),
			),
		);
		
		$this->view->contents = DomFg::render( $panel ) . DomFg::render( $outerPanel ) . DomFg::render( $borderlessPanel );
	}
	
	public function tabPanel() {
		$this->view->addPartitioner('contents', 'DomFg/Source/TabPanel.html');
		
		$panel = array(
			'id' => 'myTabPanel',
			'type' => 'tabPanel',
			'headerTabs' => true,
			'action' => $this->getUrl('tabPanel/'),
			'items' => array(
				array(
					'title' => 'First tab',
					'html' => 'First tab content',
					'bodyStyle' => 'height:150px; padding:10px',
				),
				array(
					'title' => 'Second tab',
					'html' => 'Second tab content',
					'bodyStyle' => 'height:150px; padding:10px',
				),
				array(
					'title' => 'Third tab',
					'html' => 'Third tab content',
					'bodyStyle' => 'height:150px; padding:10px',
				),
				array(
					'title' => 'Last tab',
					'html' => 'Last tab content',
					'bodyStyle' => 'height:150px; padding:10px',
				),
			),
			'buttons' => array(
				array(
					'title' => 'Jump to third tab (Does not submit form!)',
					'link' => $this->getUrl('tabPanel/&amp;myTabPaneltab=2'),
				),
				array(
					'title' => 'Submit and stay on current tab',
				),
			),
		);
		
		$this->view->contents = DomFg::render( $panel );
	}

	public function innerTabPanel() {
		$this->view->addPartitioner('contents', 'DomFg/Source/InnerTabPanel.html');
		
		$panel = array(
			'id' => 'myTabPanel',
			'type' => 'tabPanel',
			'style' => 'width:400px; float:left;',
			'headerTabs' => false,
			'border'=>true,
			'frame' => false,
			'action' => $this->getUrl('tabPanel/'),
			'items' => array(
				array(
					'title' => 'First tab',
					'html' => 'First tab content',
					'bodyStyle' => 'height:150px; padding:10px',
				),
				array(
					'title' => 'Second tab',
					'html' => 'Second tab content',
					'bodyStyle' => 'height:150px; padding:10px',
				),
				array(
					'title' => 'Third tab',
					'html' => 'Third tab content',
					'bodyStyle' => 'height:150px; padding:10px',
				),
				array(
					'title' => 'Last tab',
					'html' => 'Last tab content',
					'bodyStyle' => 'height:150px; padding:10px',
				),
			),
		);
		
		$panel2 = array(
			'id' => 'myTabPanel2',
			'type' => 'tabPanel',
			'style' => 'width:400px; float:left;',
			'headerTabs' => true,
			'frame' => false,
			'border'=>true,
			'action' => $this->getUrl('tabPanel/'),
			'items' => array(
				array(
					'title' => 'First tab 2',
					'html' => 'First tab content',
					'bodyStyle' => 'height:150px; padding:10px',
				),
				array(
					'title' => 'Second tab 2',
					'html' => 'Second tab content',
					'bodyStyle' => 'height:150px; padding:10px',
				),
				array(
					'title' => 'Third tab 2',
					'html' => 'Third tab content',
					'bodyStyle' => 'height:150px; padding:10px',
				),
				array(
					'title' => 'Last tab 2',
					'html' => 'Last tab content',
					'bodyStyle' => 'height:150px; padding:10px',
				),
			),
		);
		
		$this->view->contents = DomFg::render( $panel ) .  DomFg::render( $panel2 );
	}
	
	public function loginForm1() {
		$this->view->addPartitioner('contents', 'DomFg/Source/LoginForm1.html');
		
		$post = DomFgPost::getInstance();
		if( $post->login ) {
			
			// load user by using $post->username and $post->password
			
			$error = array(
				'type' => 'error',
				'message' => 'Teid ei õnnestunud sisse logida',
				'colspan' => 2,
			);
		}
		
		
		$panel = array(
			'title' => 'Login to enter',
			'layout' => 'table',
			'style' => 'width:400px',
			'action' => $this->getUrl('loginForm1'),
			'items' => array(
				$error,
				array(
					'type' => 'textField',
					'title' => 'Kasutajanimi',
					'name' => 'username',
					'value' => $post->username,
				),
				array(
					'type' => 'password',
					'title' => 'Parool',
					'name' => 'password',				
				),
			),	
			'buttons' => array(
				array(
					'title' => 'Logi sisse',
					'name' => 'login',
				),
			),
		);
		
		
		$this->view->contents = DomFg::render( $panel );
		
		
	}
	
	public function loginForm2() {
		$this->view->addPartitioner('contents', 'DomFg/Source/LoginForm2.html');
		
		$post = DomFgPost::getInstance();
		if( $post->login ) {
			
			// load user by using $post->username and $post->password
			
			$error = array(
				'type' => 'error',
				'message' => 'Teid ei õnnestunud sisse logida',
				'colspan' => 5,
			);
		}
		
		
		$panel = array(
			'title' => 'Login to enter',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 5,
			),
			'style' => 'width:770px',
			'action' => $this->getUrl('loginForm2'),
			'items' => array(
				$error,
				array(
					'type' => 'textField',
					'title' => 'Kasutajanimi',
					'name' => 'username',
					'value' => $post->username,
				),
				array(
					'type' => 'password',
					'title' => 'Parool',
					'name' => 'password',				
				),
				array(
					'type' => 'button',
					'title' => 'Logi sisse',
					'name' => 'login',
				),
			),
		);
		
		
		$this->view->contents = DomFg::render( $panel );
		
		
	}

	public function complexForm() {
		$this->view->addPartitioner('contents', 'DomFg/Source/ComplexForm.html');
		
		$post = DomFgPost::getInstance();
		
		if( $post->save) {
			$save = array(
				'type' => 'ok',
				'message' => 'You submitted form!'
			);
		}
		
		if( $post->cancel ) {
			$cacel =array(
				'type' => 'warning',
				'message' => 'You cancelled form!',
			);
		}
		
		// ----------------------------------
		// MAIN PANEL
		// ----------------------------------
		$panel = array(
			'type' => 'tabPanel',
			'action' => $this->getUrl('advancedFormSample1'),
			'style' => 'width: 800px; margin-left: 15px;',
			'headerTabs' => true,
		);
		
		
		// ----------------------------------
		// TAB 1
		// ----------------------------------
		$tab1 = array(
			'type' => 'panel',
			'title' => 'Public info',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 2,
				'columnWidths' => array(500,300),
			),
			'items' => array(
				array(
					'type' => 'fieldSet',
					'title' => 'Overall',
					'layout' => 'table',
					'items' => array(
						array(
							'type' => 'textField',
							'title' => 'Firstname',
							'name' => 'firstname'
						),
						array(
							'type' => 'textField',
							'title' => 'Lastname',
							'name' => 'lastname'
						),
						array(
							'type' => 'textField',
							'title' => 'Location',
							'name' => 'location'
						),
						array(
							'type' => 'Select',
							'title' => 'Age',
							'values' => array('5-18','19-25','26-39','40-..'),
							'width' => 90,
						),
						
					),
				),
				array(
					'type' => 'fieldSet',
					'title' => 'Contact',
					'layout' => 'table',
					'layoutConfig' => array(
						'columns' => 1,
					),
					'items' => array(
						array(
							'type' => 'title',
							'value' => 'Phone',
							'style' => 'text-align:left',
						),
						array(
							'type' => 'textField',
							'name' => 'phone'
						),
						array(
							'type' => 'title',
							'value' => 'Email',
							'style' => 'text-align:left',
						),
						array(
							'type' => 'textField',
							'name' => 'email'
						),
					),
				),
				array(
					'type' => 'fieldSet',
					'title' => 'Some info',
					'colspan' => 2,
					'layout' => 'table',
					'items' => array(
						array(
							'type' => 'textArea',
							'title' => 'Some info',
							'name' => 'someInfo',
							'style' => 'width:600px',
						),
					),
				),
			),
			'buttons' => array(
				array(
					'title' => 'Save',
					'name' => 'save',
					'style' => 'float:right',
				),
				array(
					'title' => 'Cancel',
					'name' => 'cancel',
					'style' => 'float:left',
				),
			),
		);
		
		// ----------------------------------
		// TAB 2
		// ----------------------------------
		$tab2 = array(
			'type' => 'fieldSet',
			'title' => 'Private info',
			'items' => array(
			
			),
		);
		
		// ----------------------------------
		// TAB 3
		// ----------------------------------
		$tab3 = array(
			'type' => 'panel',
			'title' => 'Description',
			'items' => array(
			
			),
		);
		
		// ----------------------------------
		// ADD TABS TO PANEL
		// ----------------------------------
		$panel['items'] = array( 
			array(
				'type' => 'info',
				'message' => 'Everything is okay',
				'description' => 'This message is shown before every tab',
			),
			$save,
			$cancel,
			$tab1, 
			$tab2, 
			$tab3,
			array(
				'type' => 'info',
				'message' => 'Everything is okay',
				'description' => 'This message is shown after every tab',
			),
		);
		
		$this->view->contents = DomFg::render( $panel );
	
	}

	public function allInputs() {
		$this->view->addPartitioner('contents', 'DomFg/Source/AllInputs.html');
		
		$selectObject->name = 'OBECT NAME';
		$selectObject->id = 'OBJECT ID';
		$post = DomFgPost::getInstance();
	
		$contents = array(
			'type' => 'panel',
			'title' => 'DomFg form items',
			'style' => 'margin: 10px; width: 800px;',
			'layout' => 'table',
			'action' => $this->getUrl('allInputs'),
			'layoutConfig' => array(
				'columns' => 2,
				'columnWidths' => array(150,'*'),
			),
			'items' => array(
				array(
					'type' => 'panel',
					'layout' => 'none',
					'colspan' => 2,
					'items' => array(
						array(
							'type' => 'button',
							'title' => 'Wrong button',
							'name' => 'wrongButton',
							'style' => 'float:right',
							'colspan' => 2,
						),array(
							'type' => 'button',
							'title' => 'Correct button',
							'name' => 'submitForm',
							'style' => 'float:right',
							'colspan' => 2,
						),
						
					),
				),
				array(
					'title' => 'Textfield title',
					'type' => 'textField',
					'name' => 'textfield',
					'value' => $post->textfield ? $post->textfield : 'textfield',
				),
				array(
					'title' => 'Textarea title',
					'type' => 'textArea',
					'name' => 'textarea',
					'value' => $post->textarea ? $post->textarea : 'textarea',
				),
				array(
					'title' => 'Number title',
					'type' => 'number',
					'name' => 'number',
					'value' => $post->number ? $post->number : 10.10,
				),
				array(
					'title' => 'Select title',
					'type' => 'select',
					'name' => 'select',
					'selected' => $post->select ? $post->select : 'KEY 2',
					'values' => array( 
									array('key'=>'Parent','value'=>'Parent 1', 'level'=>0),
									array('key'=>'Child 1','value'=>'Child 1', 'level'=>1),
									array('key'=>'Child 2','value'=>'Child 2', 'level'=>1),
									
									array('key'=>'KEY 1','value'=>'VALUE 1', 'level'=>1),
									array(0=>'KEY 2', 1=>'VALUE 2', 2=>2),
									'KEY 3' => 'VALUE 3',
									$selectObject,
								),
					'keyProperty' => 'key',
					'property' => 'value',
				),
				array(
					'title' => 'Multi select title',
					'type' => 'multiSelect',
					'name' => 'multiselect',
					'selected' => $post->multiselect ? arrayFlipTrue((array)$post->multiselect) : array('KEY 2'=>true,'KEY 3'=>true),
					'values' => array( 
									array('key'=>'Parent','value'=>'Parent 1', 'level'=>0),
									array('key'=>'Child 1','value'=>'Child 1', 'level'=>1),
									array('key'=>'Child 2','value'=>'Child 2', 'level'=>1),
									
									array('key'=>'KEY 1','value'=>'VALUE 1', 'level'=>1),
									array(0=>'KEY 2', 1=>'VALUE 2', 2=>2),
									'KEY 3' => 'VALUE 3',
									$selectObject,
								),
					'keyProperty' => 'key',
					'property' => 'value',
				),
				array(
					'title' => 'File title',
					'type' => 'file',
					'name' => 'file',
				),
				array(
					'title' => 'CheckBox title',
					'type' => 'checkBox',
					'name' => 'check',
					'checked' => $post->check,
				),
				array(
					'title' => 'Check box group title',
					'type' => 'checkBoxGroup',
					'name' => 'checkboxgroup',
					'checked' => $post->checkboxgroup ? $post->checkboxgroup : array('KEY 2'=>true,'KEY 3'=>true),
					'values' => array( 
									array('key'=>'KEY 1','value'=>'VALUE 1'),
									array(0=>'KEY 2', 1=>'VALUE 2'),
									'KEY 3' => 'VALUE 3',
									$selectObject,
								),
					'keyProperty' => 'key',
					'property' => 'value',
				),
				array(
					'title' => 'Vertical checkbox group title',
					'type' => 'checkBoxGroup',
					'name' => 'vcheckboxgroup',
					'vertical' => true,
					'checked' => $post->vcheckboxgroup ? $post->vcheckboxgroup : array('KEY 2'=>true,'KEY 3'=>true),
					'values' => array( 
									array('key'=>'KEY 1','value'=>'VALUE 1'),
									array(0=>'KEY 2', 1=>'VALUE 2'),
									'KEY 3' => 'VALUE 3',
									$selectObject,
								),
					'keyProperty' => 'key',
					'property' => 'value',
				),
				array(
					'title' => 'Radio group title',
					'type' => 'radioGroup',
					'name' => 'radiogroup',
					'selected' => $post->radiogroup ? $post->radiogroup : 'KEY 2',
					'values' => array( 
									array('key'=>'KEY 1','value'=>'VALUE 1'),
									array(0=>'KEY 2', 1=>'VALUE 2'),
									'KEY 3' => 'VALUE 3',
									$selectObject,
								),
					'keyProperty' => 'key',
					'property' => 'value',
				),
				array(
					'title' => 'Vertical radio group title',
					'type' => 'radioGroup',
					'name' => 'vradiogroup',
					'vertical' => true,
					'selected' => $post->vradiogroup ? $post->vradiogroup : 'KEY 2',
					'values' => array( 
									array('key'=>'KEY 1','value'=>'VALUE 1'),
									array(0=>'KEY 2', 1=>'VALUE 2'),
									'KEY 3' => 'VALUE 3',
									$selectObject,
								),
					'keyProperty' => 'key',
					'property' => 'value',
				),
				array(
					'title' => 'Range title',
					'type' => 'range',
					'name' => 'range',
					'textBefore' => 'min',
					'textMiddle' => 'px max',
					'textAfter' => 'px',
					'value1' => $post->range[0] ? $post->range[0] : 10,
					'value2' => $post->range[1] ? $post->range[1] : 15
				),
				array(
					'title' => 'Date title',
					'type' => 'date',
					'name' => 'date',
					'value' => $post->date ? $post->date : date('Y-m-d'),
				),
				array(
					'title' => 'DateTime title',
					'type' => 'dateTime',
					'name' => 'datetime',
					'value' => $post->datetime ? $post->datetime : date('Y-m-d H:i:s'),
				),
				array(
					'type' => 'title',
					'value' => 'HtmlEditor',
					'style' => 'text-align:left',
					'colspan' => 2,
				),
				array(
					'type' => 'htmlEditor',
					'value' => $post->htmlEditor ? $post->htmlEditor : 'HtmlEditor',
					'style' => 'text-align:left',
					'colspan' => 2,
					'name' => 'htmlEditor',
				),
				array(
					'type' => 'title',
					'value' => 'BBCodeEditor',
					'style' => 'text-align:left',
					'colspan' => 2,
				),
				array(
					'type' => 'BBCodeEditor',
					'value' => $post->bbcodeeditor ? $post->bbcodeeditor : 'Some custom [b]text![/b] Hello world!',
					'style' => 'text-align:left',
					'colspan' => 2,
					'name' => 'bbcodeeditor',
					'id' => 'bbcodeeditor',
				),
				array(
					'type' => 'title',
					'value' => 'Button title',
				),
				array(
					'type' => 'button',
					'name' => 'Button',
					'title' => 'Button',
				),
			),
			'buttons' => array(
				array(
					'name' => 'Button1',
					'title' => 'Button 1',
				),
				array(
					'name' => 'Button2',
					'title' => 'Button 2',
				),
				array(
					'name' => 'Button3',
					'title' => 'Button 3',
				),
			),
		);
		$this->view->contents = DomFg::render( $contents );	
	
	}
	
	public function allMessages() {
		$this->view->addPartitioner('contents', 'DomFg/Source/AllMessages.html');

		$contents = array(
			'type' => 'panel',
			'title' => 'DomFg form items',
			'style' => 'margin: 10px; width: 800px;',
			'layout' => 'table',
			'action' => $this->getUrl('submit'),
			'layoutConfig' => array(
				'columns' => 2,
				'columnWidths' => array(150,'*'),
			),
			'items' => array(
				array(
					'type' => 'locationPath',
					'path' => array(
						array(
							'name' => 'Root',
						),
						array(
							'name' => 'First level',
							'link' => $this->getCurrentUrl(),
						),
						array(
							'name' => 'Other',
							'link' => $this->getCurrentUrl(),
						),
						array(
							'name' => 'Something more',
							'link' => $this->getCurrentUrl(),
						),
						array(
							'name' => 'Element with long name.. Very long',
							'link' => $this->getCurrentUrl(),
						),
					),
					'colspan' => 2,
				),
				array(
					'type' => 'error',
					'message' => 'Error <a href="'.$this->getUrl('allItems').'"> Link </a>',
					'description' => 'Error description',
					'colspan' => 2,
				),
				array(
					'type' => 'ok',
					'message' => 'Ok <a href="'.$this->getUrl('allItems').'"> Link </a>',
					'description' => 'Status description',
					'colspan' => 2,
				),
				array(
					'type' => 'info',
					'message' => 'Info <a href="'.$this->getUrl('allItems').'"> Link </a>',
					'description' => 'Info description',
					'colspan' => 2,
				),
				array(
					'type' => 'warning',
					'message' => 'Warning <a href="'.$this->getUrl('allItems').'"> Link </a>',
					'description' => 'Warning description',
					'colspan' => 2,
				),
				array(
					'type' => 'separator',
					'title' => 'separator',
					'colspan' => 2,
				),
				array(
					'type' => 'spacer',
					'title' => 'spacer',
					'colspan' => 2,
				),
				array(
					'type' => 'notice',
					'message' => 'notice - this can be some notice to keep in mind',
					'colspan' => 2,
				),
				array(
					'type' => 'tip',
					'message' => 'tip - this can be some tip',
					'colspan' => 2,
				),
				array(
					'type' => 'value',
					'value' => 'value - this can be simple text',
					'colspan' => 2,
				),
			),
		);
		$this->view->contents = DomFg::render( $contents );
	}
	
	public function inputRefreshing() {
			
		$post = DomFgPost::getInstance();
		
		if( $post->save && $_SESSION['DomFgSample_makeError'] == false ) {
			$_SESSION['DomFgSample_makeError'] = true;
			die('Tekkis viga, palun refreshige ja veenduge, et teie lisatud väärtused säilivad');
		} else {
			unset($_SESSION['DomFgSample_makeError']);
		}
		
		if( $post->save ) {
			$message = array( 
				'type' => 'warning',
				'message' => 'Postis saadud väärtused',
				'description' => 'Väärtus 1: '.$post->name.'<br /> Väärtus 2:  '.$post->listValue[0].'<br /> Väärtus 3: '.$post->listValue[1],
				'colspan' => 2,
			);
		}
				
		$panel = array(
			'title' => 'title',
			'layout' => 'table',
			'action' => $this->getUrl('inputRefreshing/'.$id),
			'layoutConfig' => array(
				'columns' => 2,
				'columnWidths' => array( 150 ),
			),
			'items' => array(
				$message,
				array(
					'type' => 'textField',
					'title' => 'Väärtus 1',
					'value' => $post->name,
					'name' => 'name',
				),
				array(
					'type' => 'textField',
					'title' => 'Väärtus 2',
					'value' => $post->listValue[0],
					'name' => 'listValue[0]',
				),
				array(
					'type' => 'textField',
					'title' => 'Väärtus 3',
					'value' => $post->listValue[1],
					'name' => 'listValue[1]',
				),
			),
			'buttons' => array(
				array(
					'title' => 'Salvesta',
					'name' => 'save'
				),
			),
		);
				
		$this->view->contents = DomFg::render( $panel );
		
		
		
	}
	
	public function inputList() {
		$this->view->addPartitioner('contents', 'DomFg/Source/InputList.html');
		
		$post = DomFgPost::getInstance();
	
		$contents = array(
			'type' => 'panel',
			'title' => 'DomFg form items',
			'style' => 'margin: 10px; width: 800px;',
			'layout' => 'table',
			'action' => $this->getUrl('inputList'),
			'layoutConfig' => array(
				'columns' => 2,
				'columnWidths' => array(150,'*'),
			),
			'items' => array(),
			'buttons' => array(
				array(
					'name' => 'Button1',
					'title' => 'Button 1',
				),
			),
		);

		for( $i=1; $i<=10; $i++) {
			$contents['items'][] = array(
				'title' => 'CheckBox '.$i,
				'type' => 'checkBox',
				'name' => 'checkBoxes[]',
				'value' => $i*10,
				'checked' => in_array($i*10, (array)$post->checkBoxes ),
			);
		}
		$this->view->contents = DomFg::render( $contents );	
		
		
	}
	
	public function inputListReal( $id = NULL ) {
		
		// --------------------------
		// create sample categories
		try { $create = !(bool)InpCategory::query()->limit(1)->commit(); } catch( Exception $e ) { $create = true; }
		if( $create == true ) {
			InpCategory::getTable()->update();
			$cat = new InpCategory(); $cat->name = 'Category 1'; $cat->save();
			$cat = new InpCategory(); $cat->name = 'Category 2'; $cat->save();
			$cat = new InpCategory(); $cat->name = 'Category 3'; $cat->save();
			$cat = new InpCategory(); $cat->name = 'Category 4'; $cat->save();
		}
		// --------------------------
		
		
		
		
		
		
		// SAMPLE STARTS HERE
		// --------------------------
		
		
		$item = $id ? InpItem::load($id) : new InpItem();
		
		$this->view->addPartitioner('contents', 'DomFg/Source/InputListReal.html');
		
		$post = DomFgPost::getInstance();
		
		if( $post->save ) {
			
			$item->title = $post->title;
			
			
			if( $post->whichForm == 1 ) {
				$categoryValues = $post->categories_check;
			} else if( $post->whichForm == 2) {
				$categoryValues = $post->categories_multi;
			} else if( $post->whichForm == 3) {
				$categoryValues = array_keys($post->categories_checkGroup);
			}
			
			// set method takes ID or objects as input and sets all children/parents to these. 
			// meaning that every parent/child that is not in given list is removed
			// and new parents/children are added.
			$item->categories->set( $categoryValues );


			if( $item->save() ) {
				$id = $item->id;
				$message = array(
					'type' => 'ok',
					'message' => 'Saved',
					'colspan' => 2,
				);
			} else {
				$message = array(
					'type' => 'error',
					'message' => $item->getValidationMessages('<br>'),
					'colspan' => 2,
				);
			}
		}
		
		$contents = array(
			'type' => 'panel',
			'title' => 'DomFg input list working sample with DomAr',
			'style' => 'margin: 10px; width: 800px;',
			'layout' => 'table',
			'action' => $this->getUrl('inputListReal/'.$id),
			'layoutConfig' => array(
				'columns' => 2,
				'columnWidths' => array(150,'*'),
			),
			'items' => array(
				$message,
				( $item->id == 0 ? NULL : array(
					'type' => 'tip',
					'message' => 'Changing element with id '.$item->id,
					'colspan' => 2,
				)),
				array(
					'type' => 'textField',
					'title' => 'Title',
					'value' => $item->title,
					'name' => 'title',
				),
				array(
					'type' => 'radioGroup',
					'title' => 'Which form to use',
					'values' => array( 1 => 'Checkboxes', 2 => 'MultiSelect', 3 => 'Checkbox group'),
					'checked' => $post->whichForm ? $post->whichForm : 1,
					'name' => 'whichForm',
				),
			),
			'buttons' => array(
				array(
					'name' => 'save',
					'title' => 'Save',
				),
			),
		);
		
		// SINGLE CHECKBOXES
		$contents['items'][] = array(
			'type' => 'separator',
			'title' => 'Single checkboxes',
			'colspan' => 2,
		);
		$selectedCategories = $item->categories->idSetQuery('id')->commit()->toArray();
		foreach( InpCategory::idSetQuery('name') as $id => $name ) {
			$contents['items'][] = array(
				'title' => $name,
				'type' => 'checkBox',
				'name' => 'categories_check[]',
				'value' => $id,
				'checked' => (bool)$selectedCategories[$id],
			);
		}
		
		// MULTI SELECT
		// same thing can be done with multiSelect
		$contents['items'][] = array(
			'type' => 'separator',
			'title' => 'Multi select',
			'colspan' => 2,
		);
		$contents['items'][] = array(
			'type' => 'multiSelect',
			'title' => 'Categories',
			'name' => 'categories_multi',
			'values' => InpCategory::idSetQuery('name')->commit()->toArray(),
			'selected' => arrayFlipTrue( $item->categories->idSetQuery('id')->commit()->toArray() ),
		);
		
		
		// CHECKBOX GROUP
		// same thing can be done with checkBoxGroup
		$contents['items'][] = array(
			'type' => 'separator',
			'title' => 'Checkbox group',
			'colspan' => 2,
		);
		$contents['items'][] = array(
			'type' => 'checkBoxGroup',
			'title' => 'Categories',
			'name' => 'categories_checkGroup',
			'values' => InpCategory::idSetQuery('name')->commit()->toArray(),
			'selected' => arrayFlipTrue( $item->categories->idSetQuery('id')->commit()->toArray() ),
		);
		
		$this->view->contents = DomFg::render( $contents );		
		
		
	}
	
	public function tooltips() {
		$this->view->addPartitioner('contents', 'DomFg/Source/tooltips.html');
		
		DomFgToolTip::c()->title('Tooltip 1')->contents('This is sample tooltip')->elementId('tip1');
		DomFgToolTip::c()->title('Tooltip 2')->contents('<img src="'.$this->getMediaUrl('Icons/configuration.gif').'"/> This is sample tooltip <b>with html</b>')->elementId('tip2');
		
		$panel = array(
			'title' => 'DomFgToolTip',
			'layout' => 'table',
			'action' => $this->getUrl('calendar/'.$id),
			'layoutConfig' => array(
				'columns' => 2,
				'columnWidths' => array( 150 ),
			),
			'html' => '
				<div id="tip1" style="padding:10px">Tooltip sample 1</div>
				
				<div id="tip2" style="padding:10px">Tooltip sample 2</div>
			',
		);
				
		$this->view->contents = DomFg::render( $panel );
		
		
	}
	
	public function calendar() {
		$this->view->addPartitioner('contents', 'DomFg/Source/Calendar.html');
		
		$date1 = date('Y-m-d', strtotime('+1 day'));
		$date2 = date('Y-m-d', strtotime('+5 day'));
		$someData = array(
			$date1 => array('text'=>'Meet with someone', 'color'=>'red'),
			$date2 => array('text'=>'Meet with someone else', 'color'=>'blue'),	
		);

		function dayHandler( DomFgCalendarDay $day, $someData ) {
			$date = date('Y-m-d', $day->stamp );
			$day->style = 'width:100px; height:100px;';
			return '<div style="color:'.$someData[$date]['color'].'">'.$someData[$date]['text'].'</div>';
		}
		
		$panel = array(
			'title' => 'Kalender',
			'layout' => 'table',
			'style' => 'width:800px',
			'items' => array(
				array(
					'type' => 'calendar',
					'start' => date('Y-m-d'),
					'end' => date('Y-m-d', strtotime('+100 days')),
					'monthStyle' => 'border: 1px dotted lightgrey; margin-bottom: 5px; ',
					'dayHandler' => 'dayHandler',
					'dayHandlerArgs' => array($someData),
				),
			),	
		);
		$this->view->contents = DomFg::render( $panel );
	}
	
	public function customHtml() {
		$this->view->addPartitioner('contents', 'DomFg/Source/CustomHtml.html');
		
		$post = DomFgPost::getInstance();
		
		if( $post->doSearch ) {
			$message = array(
				'type' => 'warning',
				'message' => 'This search actually does nothing! You searched for <b>'.$post->search.'</b>',
				'colspan' => 2,
			);
		}
		
		$customView = new DomView( 'DomFg/customView.html');
		$customView->someParam = 'Hello world!';
		$customView->searched = $post->search;
		$customView->letters = array('A','B','C','D','E','F','G','H');
		
		
		$panel = array(
			'title' => 'Custom html content',
			'layout' => 'table',
			'action' => $this->getUrl('customHtml'),
			'layoutConfig' => array(
				'columnWidths' => array(150)
			),
			'items' => array(
				$message,
				array(
					'type' => 'textField',
					'name' => 'search',
				),
				array(
					'type' => 'button',
					'name' => 'doSearch',
					'title' => 'Search',
				),
				array(
					'colspan' => 2,
					'value' => $customView->fetch(),
				),
			),		
		);
		
		
		
		$this->view->contents = DomFg::render( $panel );
	}
	
	public function ajaxSearchSelectQuery( $search ) {
		$items = SearchSelectItem::dataQuery()
			->select('t.id, t.name, t.age')
			->where('t.name LIKE \'%'.addslashes($search).'%\'')
			->orderBy('t.name')
			->limit(20)
			->commit()->toArray();
			
		$json = '[';
		foreach( $items as $item ) {
			$json .= "{";
			$json .= "'key':'" . addslashes($item['id']) . "',";
			$json .= "'value':'" . addslashes($item['name']) . "',";
			$json .= "'showValue':'" . addslashes($item['name'] . '<b> (Age:'.$item['age'].')</b>') . "',";
			$json .= "},";
		}
		$json .= ']';
		die( $json );
	}
	
	public function ajaxSearchSelect() {
		
		SearchSelectItem::populateDatabase();
		
		$this->view->addPartitioner('contents', 'DomFg/Source/SearchSelect.html');
		
		$post = DomFgPost::getInstance();
		$selected  = array(1=>'Oliver');
		if( $post->save ) {
			$items = '';
			foreach( $post->items as $item ) {
				
				// all items key's that are added as new start with word 'new'.
				if( substr($item['key'],0,3) == 'new' ) {
					
					// check if item with this name already exists. We don't want duplicates
					$newItem = SearchSelectItem::query()->where('t.name LIKE \''.addslashes($item['value']).'\'')->limit(1)->commit();
					if( $newItem ) {
						$items .= '<b>'.$item['value'].'</b> already existed in database. Its id is: <b>'.$newItem->id.'</b><br>';
						
					// if id didn't, lets create it.
					} else {
						$newItem = new SearchSelectItem();
						$newItem->name = $item['value'];
						$newItem->age = mt_rand(20,60);
						$newItem->save();
						$items .= '<b>'.$item['value'].'</b> was added to database. Its id is: <b>'.$newItem->id.'</b><br>';
					}
					
					$selected[$newItem->id] = $newItem->name;
					
				// item was not added as new. So it should exist in database.
				} else {
					$items .= '<b>'.$item['value'].'</b> with id <b>'.$item['key'].'</b><br>';
					$selected[$item['key']] = $item['value'];
				}
			}
			$message = array(
				'type' => 'info',
				'colspan' => 3,
				'message' => 'You selected these items: ',
				'description' => $items,
			);
		}
		
		$panel = array(
			'title' => 'Search select',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 3,
			),
			'style' => 'width:500px',
			'action' => $this->getUrl('ajaxSearchSelect'),
			'items' => array(
				$message,
				array(
					'type' => 'searchSelect',
					'name' =>  'items',
					'maxElements' => 8,
					'allowAddingNew' => true,
					'selected' => $selected,
					'ajaxUrl' => $this->getUrl('ajaxSearchSelectQuery/[$search]'),
				),
				array(
					'type' => 'button',
					'title' => 'Save',
					'name' => 'save',
				),
			),	
		);
		$this->view->contents = DomFg::render( $panel );
	}
	
	
	public function simpleGrid() {
		$this->view->addPartitioner('contents', 'DomFg/Source/SimpleGrid.html');

		$post = DomFgPost::getInstance('mySearchName');
		
		$arrayValue = array('key'=>array('value'=>'VALUE'));
		$source = new DomFgDataSource(array(
			array('id'=>0,'name'=>'Oliver','age'=>15,'array'=>$arrayValue),
			array('id'=>2,'name'=>'Getter','age'=>25,'array'=>$arrayValue),
			array('id'=>3,'name'=>'Jaanika','age'=>12,'array'=>$arrayValue),
			array('id'=>5,'name'=>'SSS','age'=>54,'array'=>array()),
			array('id'=>6,'name'=>'Oliver','age'=>15,'array'=>$arrayValue),
			array('id'=>7,'name'=>'Mari','age'=>23,'array'=>$arrayValue),
			array('id'=>8,'name'=>'Getter','age'=>25,'array'=>$arrayValue),
			array('id'=>9,'name'=>'SSSS','age'=>12,'array'=>$arrayValue),
			array('id'=>0,'name'=>'Oliver','age'=>15,'array'=>$arrayValue),
			array('id'=>1,'name'=>'Mari','age'=>23,'array'=>$arrayValue),
			array('id'=>2,'name'=>'Getter','age'=>25,'array'=>$arrayValue),
			array('id'=>3,'name'=>'Jaanika','age'=>12,'array'=>$arrayValue),
			array('id'=>4,'name'=>'Anne','age'=>23,'array'=>$arrayValue),
			array('id'=>5,'name'=>'Wrony','age'=>54,'array'=>array()),
			array('id'=>6,'name'=>'SSSSA','age'=>15,'array'=>$arrayValue),
			array('id'=>7,'name'=>'Mari','age'=>23,'array'=>$arrayValue),
			array('id'=>8,'name'=>'Getter','age'=>25,'array'=>$arrayValue),
			array('id'=>9,'name'=>'Jaanika','age'=>12,'array'=>$arrayValue),
			array('id'=>0,'name'=>'Oliver','age'=>15,'array'=>$arrayValue),
			array('id'=>1,'name'=>'Mari','age'=>23,'array'=>$arrayValue),
			array('id'=>2,'name'=>'Getter','age'=>25,'array'=>$arrayValue),
			array('id'=>3,'name'=>'Jaanika','age'=>12,'array'=>$arrayValue),
			array('id'=>4,'name'=>'SSSSS','age'=>23,'array'=>$arrayValue),
			array('id'=>5,'name'=>'Wrony','age'=>54,'array'=>array()),
			array('id'=>6,'name'=>'Oliver','age'=>15,'array'=>$arrayValue),
			array('id'=>7,'name'=>'Mari','age'=>23,'array'=>$arrayValue),
			array('id'=>8,'name'=>'Getter','age'=>25,'array'=>$arrayValue),
			array('id'=>9,'name'=>'Jaanika','age'=>12,'array'=>$arrayValue),
		
		), 18);
		$source->searchKeys( array('name','age') );

		if( $post->doSearch ) {
			$post->store('mySearchName',array('doSearch','searchWord'));
			$source->where( $post->searchWord );
		} 
			
		if( isset($_GET['doSomethingValue']) ) {
			$warning = array(
				'type' => 'warning',
				'message' => 'You wanted to do something with element nr '.$_GET['doSomethingValue'],
				'colspan' => 3,
			);
		}
			
			
		function ageHandler( $value ) {
			return $value.' aastat';
		}	
		
		function nameHandler( &$row, $i ) {
			DomFgToolTip::c()->title($row['name'])->contents('This is sample usage of tooltips in a grid')->elementId('tip_'.$i);
			return '<div id="tip_'.$i.'">'.$row['name'].'</div>';
		}
		
		$grid = array(
			'title' => 'Grid',
			'style' => 'width: 800px',
			'action' => $this->getUrl('simpleGrid/'),
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 3,
				'columnWidths' => array(150,150),
			),
			'items' => array(
				$warning,
				array(
					'type' => 'title',
					'value' => 'Otsi',
				),
				array(
					'type' => 'textField',
					'name' => 'searchWord',
					'value' => $post->searchWord,
				),
				array(
					'type' => 'button',
					'name' => 'doSearch',
					'title' => 'Otsi',
				),
				array(
					'type' => 'grid',
					'name' => 'SampleGrid',
					'colspan' => 3,
					'link' => $this->getUrl('simpleGrid'),
					'source' => $source,
					'fields' => array(
						array(
							'title' => 'Name',
							'link' => $this->getUrl('editGridItem/'),
							'handler' => 'nameHandler',
						),
						array(
							'title' => 'Age',
							'key' => 'age',
							'handler' => 'ageHandler',
						),
						array(
							'title' => 'Some value',
							'key' => array('array','key','value'),
						),
						array(
							'type' => 'gridButton',
							'confirmMessage' => 'Are you sure you want to do that?',
							'value' => 'Press me',
							'link' => $this->getUrl('simpleGrid/&doSomethingValue=[$id]'),
						),
					),
				),
			),
		);
		
		
		$this->view->contents = DomFg::render( $grid );
	
	}
	
	public function advancedGrid() {
		
		$this->view->addPartitioner('contents', 'DomFg/Source/AdvancedGrid.html');
		
		GridPerson::createSampleData();
		
		
		if( $_GET['delete'] && $item = GridPerson::load( $_GET['delete'] )){
			$item->delete();
		}
		
		if( $_GET['moveUp'] && $item = GridPerson::load($_GET['moveUp']) ) {
			$item->order->decrease();
		}
		
		if( $_GET['moveDown'] && $item = GridPerson::load($_GET['moveDown']) ) {
			$item->order->increase();
		}
		
		if( $_GET['change'] ){
			$message = array(
				'type' => 'warning',	
				'message' => 'Muutmist ei ole!',
			);
		}
		
		DomFgPost::getInstance('advancedGrid')->store('advancedGrid',array('personsGrid'));

		$post = DomFgPost::getInstance();
		if( $post->save ) {
			foreach( $post->order as $id => $i ) {
				$item = GridPerson::load( $id );
				$item->order->setPos( $i );
				$item->save();
			}
		}
		
		$query = GridPerson::query();
		switch( $post->personsGrid['orderByField'] ) {
			case 'name': $query->orderBy('t.lastname, t.firstname '.$post->personsGrid['orderByDirection']); break;
			case 'age': $query->orderBy('t.age '.$post->personsGrid['orderByDirection']); break;
			case 'group': $query->orderBy('t.group '.$post->personsGrid['orderByDirection']); break;
			case 'money': $query->orderBy('t.money '.$post->personsGrid['orderByDirection']); break;
			case 'spent': $query->orderBy('t.spent '.$post->personsGrid['orderByDirection']); break;
			default: $query->orderBy('t.group, t.order '.$post->personsGrid['orderByDirection']); break;
		}
		
		$source = new DomArDataSource( $query, 30 );
		
		function handler_name( $person ) {
			return $person['firstname'].' '.$person['lastname'];
		}
		
		function handler_order( $row, $i, $controller ) {
			$field  = array(
				'type' => 'number',	
				'value' => $row['order'],
				'name' => 'order['.$row['id'].']',
			);
			$html = ' 
				<a href="'.$controller->getUrl('advancedGrid').'&amp;pg='.$_GET['pg'].'&amp;moveUp='.$row['id'].'">Üles</a> 
				<a href="'.$controller->getUrl('advancedGrid').'&amp;pg='.$_GET['pg'].'&amp;moveDown='.$row['id'].'">Alla</a> ';
			return DomFg::render( $field ) . $html;
		}
		
		function handler_row( GridPerson $person, $color ) {
			$result = array();
			$result['id'] = $person->id;
			$result['firstname'] = $person->firstname;
			$result['lastname'] = $person->lastname;
			$result['age'] = $person->age;
			$result['order'] = $person->order->pos;
			$result['group'] = $person->group;
			$result['money'] = $person->money;
			$result['spent'] = $person->spent.' ('.round($person->spent * 100 / $person->money).'%)';
			return $result;
		}
		
		$panel = array(
			'title' => 'title',
			'layout' => 'table',
			'action' => $this->getUrl('advancedGrid'),
			'layoutConfig' => array(
				'columns' => 1,
			),
			'items' => array(
				$message,
				array(
					'name' => 'personsGrid',
					'type' => 'grid',
					'source' => $source,
					'link' => $this->getUrl('advancedGrid'),
					'rowHandler' => 'handler_row',
					'rowHandlerArgs' => array( 'red' ),
					'fields' => array(
						array('handler' => 'handler_name', 'title' => 'Nimi', 'orderByField' => 'name'),
						array('key' => 'age', 'title' => 'Vanus', 'orderByField' => 'age'),
						array('key' => 'group', 'title' => 'Grupp', 'orderByField' => 'group'),
						array('key' => 'money', 'title' => 'Raha', 'orderByField' => 'money'),
						array('key' => 'spent', 'title' => 'Kulutatud', 'orderByField' => 'spent'),
						array('handler' => 'handler_order', 'handlerArgs' => array( $this ), 'title' => 'Jrk', 'orderByField' => 'jrk'),
						array('type' => 'gridButton', 'value' => 'Muuda', 'link' => $this->getUrl('advancedGrid/&amp;change='), 'title' => 'Valikud','colspan' => 2),
						array('type' => 'gridButton', 'value' => 'Kustuta', 'link' => $this->getUrl('advancedGrid/&amp;delete='), 'confirmMessage' => 'Are you sure?' ),
					),
				),
				array(
					'type' => 'button',	
					'style' => 'margin-left:400px;',
					'title' => 'Salvesta',
					'name' => 'save',
				),
			),
		);
					
		$this->view->contents = DomFg::render( $panel );
		
		
		
	}
	
	public function imageGrid() {
		
		ImageGridImage::createSampleData();
		
		
		$this->view->addPartitioner('contents', 'DomFg/Source/ImageGrid.html');

		$post = DomFgPost::getInstance('mySearchName');
		$query = ImageGridImage::query()->orderBy('t.`order`');
		
		if( $_GET['deleteImage'] && $image = ImageGridImage::load($_GET['deleteImage']) ) {
			$image->delete();
		}
		
		if( $post->addImage ) {
			$image = new ImageGridImage();
			$image->title = $post->title;
			$image->icon->addUploaded( $post->image[0], $post->image[1] );
			$image->save();
		}
		
		if( $post->save ) {
			foreach( $query->commit() as $i => $image ) {
				$image->order = $post->images['order'][$i];
				$image->save();
			}
		}
		
		function getIcon( &$row ) {
			return $row->icon->toHtml();
		}
		
		function makeBold( $value ) {
			return '<b>'.$value.'</b>';
		}

		$grid = array(
			'title' => 'ImageGrid',
			'style' => 'width: 800px',
			'action' => $this->getUrl('imageGrid/'),
			'layout' => 'table',
			'items' => array(
				array(
					'colspan' => 2,
					'type' => 'info',
					'message' => 'ImageGrid',
					'description' => 'Notice that image grid also allows reordering images. You can drag images to reorder them!.<br> If you delete all images then 20 images will be automatically added to this gallery.'
				),
				array(
					'type' => 'fieldSet',
					'layout' => 'table',
					'colspan'  => 2,
					'title' => 'Add new image',
					'items' => array(
						array(
							'title' => 'Title',
							'type' => 'textField',	
							'name' => 'title',
						),
						array(
							'title' => 'Image file',
							'type' => 'file',	
							'name' => 'image',
						),
						array('type'=>'value'),
						array(
							'type' => 'button',
							'title' => 'Add',
							'name' => 'addImage',
						),
					),
				),
				array(
					'type' => 'imageGrid',
					'id' => 'images',
					'name' => 'images',
					'colspan' => 2,
					'ordering' => true,
					'link' => $this->getUrl('simpleGrid'),
					'source' => new DomArDataSource( $query, 99999),
					'fields' => array(
						array('handler'=>'getIcon'),
						array('key' => 'title','handler'=>'makeBold'),
						array('type' => 'gridButton','value' => 'Delete','link' => $this->getUrl('imageGrid/&deleteImage='),'confirmMessage' => 'Are you sure?',),
					),
				),
			),
			'buttons' => array(
				array(
					'title' => 'Save',
					'name' => 'save',
				),
			),
		);
		
		
		$this->view->contents = DomFg::render( $grid );
	}
	
	

	
	public function htmlEditorImages() {
		
		$this->view->addPartitioner('contents', 'DomFg/Source/HtmlEditorImages.html');
		
		$post = DomFgPost::getInstance();
		if( $post->save ) {
			
		}
		
		$panel = array(
			'title' => 'Html editor images',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 2,
			),
			'style' => 'width:700px',
			'action' => $this->getUrl('htmlEditorImages'),
			'items' => array(
				$message,
				array(
					'type' => 'htmlEditor',
					'title' => 'Editor',
					'name' => 'text',
					'imageManagerUrl' => $this->getUrl('htmlImagesHandler'),
				),
			),
			'buttons' => array(
				array(
					'type' => 'button',
					'title' => 'Save',
					'name' => 'save',
				),
			),	
		);
		$this->view->contents = DomFg::render( $panel );
		
	}
	
	public function htmlImagesHandler() {
		
		HtmlEditorImage::getTable()->update();
		
		$this->view->setTemplate('simple.html');
		
		if( $_GET['delete'] && $item = HtmlEditorImage::load( $_GET['delete'] )){
			$item->delete();
		}
						
		$source = new DomArDataSource( HtmlEditorImage::query()->orderBy('t.name DESC'), 40 );
		
		function addHandler( $row ) {
			return '<a href="javascript:updateHtmlEditorImage('.$row->id.',\''.$row->image->getUrl().'\');">Use</a>';
		}
		
		$panel = array(
			'title' => 'Images',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 1,
			),
			'items' => array(
				array(
					'type' => 'button',
					'title' => 'Lisa uus',
					'link' => $this->getUrl('htmlImagesEdit/'),
				),
				array(
					'type' => 'grid',
					'source' => $source,
					'fields' => array(
						array('key' => 'name', 'title' => 'Nimi'),
						array('handler'=>'addHandler','title' => 'Valikud','colspan' => 3),
						array('type' => 'gridButton', 'value' => 'Muuda', 'link' => $this->getUrl('htmlImagesEdit/')),
						array('type' => 'gridButton', 'value' => 'Kustuta', 'link' => $this->getUrl('htmlImagesHandler/&delete='), 'confirmMessage' => 'Are you sure?' ),
					),
				),
			),
		);

		$this->view->contents = DomFg::render( $panel );
		
	}
	
	public function htmlImagesEdit( $id = NULL) {
		$this->view->setTemplate('simple.html');
		
		$new = $id == 0;
		$item = $new ? new HtmlEditorImage() : HtmlEditorImage::load($id);
				
		$post = DomFgPost::getInstance();
		
		if( $post->save ) {
			
			$item->name = $post->name;
			$item->image->addUploaded( $post->image[0], $post->image[1]);
			
			if( $item->save() ) {
				$id = $item->id;
				$message = array('type' => 'ok','colspan' => 3,'message' => 'Salvestatud',);
			} else {
				$message = array('message' => 'Tekkinud on viga!','type' => 'ok','colspan' => 3,'description' => implode('<br>',$item->getValidationReport()->messages),);
			}
		}
				
		$panel = array(
			'title' => 'Image',
			'layout' => 'table',
			'action' => $this->getUrl('htmlImagesEdit/'.$id),
			'layoutConfig' => array(
				'columns' => 3,
				'columnWidths' => array( 100 ),
			),
			'items' => array(
				$message ,
				array(
					'type' => 'image',	
					'src' => $item->icon->getUrl(),
					'rowspan' => 2,
					'style' => 'width:100px; height:100px;'
				),
				array(
					'type' => 'textField',
					'title' => 'Nimi',
					'value' => $item->name,
					'name' => 'name',
				),
				array(
					'type' => 'file',
					'title' => 'File',
					'name' => 'image',
				),
			),
			'buttons' => array(
				array(
					'title' => 'Salvesta',
					'name' => 'save'
				),
				array(
					'title' => 'Back',
					'link' => $this->getUrl('htmlImagesHandler'),
					'style' => 'float:left',
				),
			),
		);
				
		$this->view->contents = DomFg::render( $panel );
	}
	
	public function fileBrowser() {
		
		$this->view->addPartitioner('contents', 'DomFg/Source/FileBrowser.html');
		
		$post = DomFgPost::getInstance();
		if( $post->save ) {
			$message = array(
				'type' => 'info',
				'colspan' => 3,
				'message' => 'Added file',
				'description' => 'Path: <b>'.$post->file[0].'</b> <br> filename:<b>'.$post->file[1].'</b>',
			);
		}
		
		$panel = array(
			'title' => 'File browser',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 3,
			),
			'style' => 'width:500px',
			'action' => $this->getUrl('fileBrowser'),
			'items' => array(
				$message,
				array(
					'type' => 'fileBrowser',
					'fileBrowserUrl' => SITE_URL.'/fileBrowser.php',
					'title' => 'Server file',
					'name' => 'file',
					'rootPath' => SITE_PATH.'files/Upload/',
					'allowedTypes' => array('__folders'=>true,'__allFiles'=>true, 'jpg'=>true),
				),
				array(
					'type' => 'button',
					'title' => 'Save',
					'name' => 'save',
				),
			),	
		);
		$this->view->contents = DomFg::render( $panel );
		
	}
		
	public function upload() {
		
		UploadImage::createSampleData();
		
		$this->view->addPartitioner('contents', 'DomFg/Source/Upload.html');

		if( $_GET['deleteImage'] && $image = UploadImage::load($_GET['deleteImage']) ) {
			$image->delete();
		}

		function getIcon( &$row ) {
			return $row->icon->toHtml();
		}
		$panel = array(
			'title' => 'Multiple files upload',
			'layout' => 'table',
			'action' => $this->getUrl('upload/'.$id),
			'layoutConfig' => array(
				'columns' => 2,
				'columnWidths' => array( 150 ),
			),
			'items' => array(
				array(
					'type' => 'upload',
					'handlerUrl' => $this->getUrl('handleUpload/'),
					'refreshUrl' => $this->getUrl('upload/'),
					'fileTypes' => '*.png;*.jpg;*.gif;',
					'name' => 'image',
					'title' => 'Pildid',
				),
				array(
					'type' => 'imageGrid',
					'colspan' => 2,
					'ordering' => false,
					'source' => new DomArDataSource( UploadImage::query(), 99999),
					'fields' => array(
						array('handler'=>'getIcon'),
						array('type' => 'gridButton','value' => 'Delete','link' => $this->getUrl('upload/&deleteImage='),'confirmMessage' => 'Are you sure?',),
					),
				),
			),
		);
		
				
		$this->view->contents = DomFg::render( $panel );
	}
	
	public function handleUpload() {
		$this->view->setTemplate(false); // disable temlate
		$im = new UploadImage();
		$im->icon->addUploaded( $_FILES['Filedata']['tmp_name'], $_FILES['Filedata']['name']);
		$im->save();
		echo '1'; // give atleast some output
		exit(0); // exit script successfully
	}
	
	public function listStarter() {
		$post = DomFgPost::getInstance();
		

		$customView = new DomView( 'DomFg/starter-list.html');
		
		
		$panel = array(
			'title' => 'list starter',
			'layout' => 'table',
			'action' => $this->getUrl('customHtml'),
			'layoutConfig' => array(
				'columnWidths' => array(150)
			),
			'bodyStyle' => 'padding:15px',
			'html' => $customView->fetch(),
		);
		
		
		
		$this->view->contents = DomFg::render( $panel );
	}
	
	public function listSearchStarter() {
		$post = DomFgPost::getInstance();
		
		

		$customView = new DomView( 'DomFg/starter-search-list.html');
		
		
		$panel = array(
			'title' => 'list starter',
			'layout' => 'table',
			'action' => $this->getUrl('customHtml'),
			'layoutConfig' => array(
				'columnWidths' => array(150)
			),
			'bodyStyle' => 'padding:15px',
			'html' => $customView->fetch(),
		);
		
		
		
		$this->view->contents = DomFg::render( $panel );
	}
	
	public function formStarter() {
		$post = DomFgPost::getInstance();
		

		$customView = new DomView( 'DomFg/starter-form.html');
		
		
		$panel = array(
			'title' => 'list starter',
			'layout' => 'table',
			'action' => $this->getUrl('customHtml'),
			'layoutConfig' => array(
				'columnWidths' => array(150)
			),
			'bodyStyle' => 'padding:15px',
			'html' => $customView->fetch(),
		);
		
		
		
		$this->view->contents = DomFg::render( $panel );
	}
	
	public function tree() {
		
		TreeItem::createSampleData();
		
		$this->view->addPartitioner('contents', 'DomFg/Source/Tree.html');
		
		function nameCall ( DomFgTreeNode $node, $element, DomController $controller ) {
			$node->name($element['name']);
			$node->link( $controller->getUrl() );
			$node->icon( $node->isLeaf() ? 'file' : 'folder');
		}

		$panel = array(
			'title' => 'Tree',
			'layout' => 'table',
			'style' => 'width:270px; float:left;',
			'layoutConfig' => array(
				'columns' => 1,
			),
			'action' => $this->getUrl('pools/'),
			'items' => array(
				array(
					'id' => 'itemsTree',
					'type' => 'tree',
					'source' => array(
						'query' => TreeItem::dataQuery()->orderBy('t.name DESC'),
						'parentIdField' => 'parent',
						'idField' => 'id',
						'nameField' => 'name',
						'callback' => 'nameCall',
						'callbackArgs' => array($this),
					),
				),
			),
		);
		
		
		
		
		
		
		
		
		function generateTreeSource( DomController $controller, $parentId = NULL, $parentNode = NULL ) {
			$list = array();
			$query = TreeItem::query()
				->where( 't.parent '.($parentId ? '='.(int)$parentId : '<=> NULL') )
				->orderBy('t.name DESC');
				
			foreach( $query->commit() as $element ) {
				$node = new DomFgTreeNode( $element->name );
				generateTreeSource( $controller, $element->id, $node );
				$node->link( $controller->getUrl() );
				if( $parentNode ) $parentNode->add( $node );
				$list[] = $node;
			}
			return $list;
		}
		
		$panel2 = array(
			'title' => 'Tree 2',
			'layout' => 'table',
			'style' => 'width:270px; float:left;',
			'layoutConfig' => array(
				'columns' => 1,
			),
			'action' => $this->getUrl('pools/'),
			'items' => array(
				array(
					'id' => 'itemsTree2',
					'type' => 'tree',
					'source' => generateTreeSource( $this ),
				),
			),
		);
		
	
		$this->view->contents = DomFg::render( $panel ) . DomFg::render( $panel2 );
	}
	
	
	public function eclipse_using() {
		$view = new DomView('DomFg/Source/eclipse_using.html');
		$panel = array(
			'title' => 'Basics',
			'bodyStyle' => 'padding: 10px;; line-height:1.8;',
			'html' =>$view->fetch(),
		);
		$this->view->contents = DomFg::render( $panel );
	}
		
	public function eclipse_DomFgF() {
		$view = new DomView('DomFg/Source/eclipse_DomFgF.html');
		$panel = array(
			'title' => 'Basics',
			'bodyStyle' => 'padding: 10px;; line-height:1.8;',
			'html' =>$view->fetch(),
		);
		$this->view->contents = DomFg::render( $panel );
	}
	
	public function ecplipse_DomFgTF() {
		$view = new DomView('DomFg/Source/eclipse_DomFgTF.html');
		$panel = array(
			'title' => 'Basics',
			'bodyStyle' => 'padding: 10px;; line-height:1.8;',
			'html' =>$view->fetch(),
		);
		$this->view->contents = DomFg::render( $panel );
	}
	
	public function ecplipse_DomFgL() {
		$view = new DomView('DomFg/Source/eclipse_DomFgL.html');
		$panel = array(
			'title' => 'Basics',
			'bodyStyle' => 'padding: 10px;; line-height:1.8;',
			'html' =>$view->fetch(),
		);
		$this->view->contents = DomFg::render( $panel );
	}
	
	public function ecplipse_DomFgSL() {
		$view = new DomView('DomFg/Source/eclipse_DomFgSL.html');
		$panel = array(
			'title' => 'Basics',
			'bodyStyle' => 'padding: 10px;; line-height:1.8;',
			'html' =>$view->fetch(),
		);
		$this->view->contents = DomFg::render( $panel );
	}
	
	
	
	public function error404() {
		$this->loadController('Index')->error404();
	}
}
?>